﻿using EventBusDemo.Api.Applications.Orders.Events;
using EventBusDemo.Api.Domains.Orders.Services;
using EventBusDemo.EventBus.Abstractions.Distributed;

namespace EventBusDemo.Api.Applications.Orders;

public class OrderAppService
{
    private readonly IDistributedEventBus _distributedEventBus;
    private readonly OrderService _orderService;

    public OrderAppService(
        IDistributedEventBus distributedEventBus,
        OrderService orderService)
    {
        _distributedEventBus = distributedEventBus;
        _orderService = orderService;
    }

    public async Task<Guid> CreateOrder(string name)
    {
        var order = await _orderService.CreateOrder(name);

        await _distributedEventBus.PublishAsync("OrderCreated", new OrderCreatedIntegrationEvent()
        {
            Id = order.Id,
            Name = name
        });

        return order.Id;
    }
}
